package com.mew.chess.core.game.bjl.game.task;

import com.mew.chess.core.common.handler.ChessPushMessageUtil;
import com.mew.chess.core.game.bjl.game.BjlGameManager;
import com.mew.chess.core.game.bjl.game.BjlRoomManager;
import com.mew.chess.core.game.bjl.game.common.BjlConstantValue;
import com.mew.chess.core.game.bjl.game.common.BjlLinkType;
import com.mew.chess.core.game.bjl.game.common.BjlPushCMDMapType;
import com.mew.chess.core.game.bjl.game.core.BjlCardUtil;
import com.mew.chess.core.game.bjl.game.message.PushShuffleCardMessage;
import com.mew.common.util.NumberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**洗牌阶段
 * @author dylin
 * @create 2021/4/22
 */
@Component
@Slf4j
public class BjlShuffleCardTask {

    public boolean shuffle(int roomId){
        BjlRoomManager roomController = BjlGameManager.getInstance().getController(roomId);
        roomController.getOnlineUser().forEach((k, v) -> {
            if (null == v || v.isWeakClose()){
                roomController.clearOnlineUser(k);
            }
        });
        if (roomController.getOnlineUser().isEmpty()){
            log.info("Bjl roomId {} not real user is stop ", roomId);
            roomController.stopGame();
            return false;
        }

        //使用的牌数
        int cardNumber= NumberUtil.randomIntByStr(BjlConstantValue.USE_CARD_NUMBER_SEC,"-");
        roomController.setCardLib(new ArrayList<>(BjlCardUtil.getShuffleCardLib().subList(0,cardNumber).stream().collect(Collectors.toList())));
        roomController.initInfo();
        roomController.getHistoryList().clear();//洗牌清除历史记录
        PushShuffleCardMessage shuffleCardMessage=roomController.getPushShuffleCardMessage();
        shuffleCardMessage.setIno(roomController.getIno());
        shuffleCardMessage.setCardNumber(cardNumber);
        shuffleCardMessage.setBigPlay(roomController.getBigRList().values());
        ChessPushMessageUtil.broadcastMessage(roomController.getOnlineUser().values(), BjlPushCMDMapType.ShuffleCardMessage.code(), shuffleCardMessage);
        roomController.changeLink(BjlLinkType.ShuffleCard.code(),BjlConstantValue.SHUFFLE_TIME);
        return true;
    }
}
